package com.demo.controller;


import com.demo.entity.Area;
import com.demo.entity.Record;
import com.demo.entity.base.Result;
import com.demo.service.AreaService;
import com.demo.service.PersonService;
import com.demo.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ymj
 * @since 2020-11-23
 */
@RestController
@CrossOrigin
@RequestMapping("/demo/record")
public class RecordController extends BaseController<Record, RecordService> {
    private RecordService recordService;
    private AreaService areaService;
    private PersonService personService;
    @Autowired
    public void setRecordService(RecordService recordService){

        this.recordService = recordService;
        this.manager = this.recordService;
    } @Autowired
    public void setAreaService(AreaService areaService){

        this.areaService = areaService;
    }
    @Autowired
    public void setPersonService(PersonService personService){

        this.personService = personService;
    }

    public Map findByPage(Integer pageNow, Integer pageSize) {
        // 参数一：当前页
        // 参数二：页面大小
        Map<String, Object> result = new HashMap<>();
        pageNow = pageNow == null ? 1 : pageNow; // 不传当前页数默认为1
        pageSize = pageSize == null ? 4 : pageSize; // 不传当前页面显示条数默认显示4条
        List<Record> data = recordService.findByPage(pageNow, pageSize); // 分页查询
        result.put("data", data);
        result.put("total", manager.count());
        return result;
    }
    @GetMapping("/findByPageByP_id")
    public Map<String, Object> findByPageByP_id(Integer pageNow, Integer pageSize,String p_id) {
        Map<String, Object> result = new HashMap<>();
        pageNow = pageNow == null ? 1 : pageNow; // 不传当前页数默认为1
        pageSize = pageSize == null ? 4 : pageSize; // 不传当前页面显示条数默认显示4条
        List<Record> data = recordService.findByPageByP_id(pageNow, pageSize,Long.valueOf(p_id)); // 分页查询
        Long totals = recordService.findTotalsByP_id(Long.valueOf(p_id));
        result.put("data", data);
        result.put("total", totals);
        return result;
    }
    @GetMapping("/findByPageByA_id")
    public Map<String, Object> findByPageByA_id(Integer pageNow, Integer pageSize,String a_id) {
        Map<String, Object> result = new HashMap<>();
        pageNow = pageNow == null ? 1 : pageNow; // 不传当前页数默认为1
        pageSize = pageSize == null ? 4 : pageSize; // 不传当前页面显示条数默认显示4条
        List<Record> data = recordService.findByPageByA_id(pageNow, pageSize,Long.valueOf(a_id)); // 分页查询
        Long totals = recordService.findTotalsByA_id(Long.valueOf(a_id));
        result.put("data", data);
        result.put("total", totals);
        return result;
    }

    @PostMapping("/findAllRecord")
    public Map findAllRecord() {
        Map<String, Object> result = new HashMap<>();
        List<Record> list = recordService.findAll();
        result.put("data", list);
        result.put("total", manager.count());
        return result;
    }
    @GetMapping("/pass")
    public Result pass(Long personId,Long areaId,String direction ){
        Record record = new Record();
        Date time = new Date();
        String allRole;
        record.setAId(areaId);
        record.setPId(personId);
        record.setDirection(direction);
        record.setRecordDate(new Date());
        Area area = areaService.getById(areaId);
        Date beginTime = area.getBeginTime();
        Date endTime = area.getEndTime();
        time.setYear(beginTime.getYear());
        time.setMonth(beginTime.getMonth());
        time.setDate(beginTime.getDate());
        String areaRole = area.getRole();
        int personRole = personService.getById(personId).getRole();
        switch (personRole){
            case 1:allRole ="ABCE";break;
            case 2:allRole ="ABCE";break;
            case 3:allRole ="ABCDE";break;
            case 4:allRole ="A";break;
            default: allRole ="F";break;
        }
        //recordService.save(record);
        if(direction.equals("A")){
            areaService.addCount(areaId);
        }else if (direction.equals("B")){
            areaService.subCount(areaId);
        }
        if (allRole.indexOf(areaRole)<0) {
            record.setStatus("C");
            recordService.save(record);
            return Result.success("越权通过");
        }
        if (beginTime.getTime()<time.getTime()&&time.getTime()<endTime.getTime()){
            record.setStatus("A");
            recordService.save(record);
            return Result.success("通过成功");
        }
        else{
            record.setStatus("B");
            recordService.save(record);
            return Result.success("非正常时间通过");
        }
    }
}

